<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>上线红包</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
    <meta name="renderer" content="webkit">
    <link href="${pageContext.request.contextPath}/static/css/common.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/static/plugins/layer/skin/layer.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/static/plugins/bootstrap-3.3.5/css/bootstrap.min.css"
          rel="stylesheet">
          <link href="${pageContext.request.contextPath}/static/plugins/select2-4.0.0/dist/css/select2.min.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/static/plugins/font-awesome-4.3.0/css/font-awesome.min.css"
          rel="stylesheet">
          <link rel="stylesheet" href="${pageContext.request.contextPath}/static/plugins/jquery.time/jquery.timepicker.min.css">
    <script src="${pageContext.request.contextPath}/static/js/jquery-1.11.3.min.js"></script>
    <script src="${pageContext.request.contextPath}/static/plugins/bootstrap-3.3.5/js/bootstrap.min.js"></script>
    <script src="/static/plugins/bootstrap-3.3.5/js/bootstrap.min.js"></script>
    <script src="${pageContext.request.contextPath}/static/plugins/layer/layer.js" type="text/javascript"></script>
	<script src="${pageContext.request.contextPath}/static/plugins/select2-4.0.0/dist/js/select2.min.js"></script>
	<script src="${pageContext.request.contextPath}/static/plugins/My97DatePicker/WdatePicker.js"
        type="text/javascript"></script>
	<script src="${pageContext.request.contextPath}/static/plugins/jquery.time/jquery.timepicker.min.js"
        type="text/javascript"></script>
	<script type="text/javascript" src="${pageContext.request.contextPath}/static/js/global.js"></script>
<body>
<div class="col-sm-12">
    <form class="form-horizontal m-t" id="form" method="post" action="/admin/redPacket_up.action">
           <input type="hidden" value="${payRedEnvRule.sid}" name="sid">
           <input type="hidden" value="${payRedEnvRule.agtCode}" name="agtCode">
            <div class="form-group">
	            <label class="col-sm-3  control-label"><i class="require">*</i>红包名称:</label>
	            <div class="col-sm-8">
	                <input class="form-control" name="rName" size="30" value="${payRedEnvRule.rName}" disabled="disabled"/>
	            </div>
            </div>
            <div class="form-group">
            <label class="col-sm-3 control-label"><i class="require">*</i>红包类型:</label>
            <div class="col-sm-8">
                <div class="col-sm-2 m-t2" style="width: 150px;">
                    <input type="radio"  name="rType" size="30" value="0" disabled="disabled"/><label>金额平均</label>
                </div>
                <div class="col-sm-2 m-t2" style="width: 150px;">
                    <input type="radio" checked name="rType" size="30" value="1" disabled="disabled"/><label>金额随机</label>
                </div>
            </div>
            </div>
            <div class="form-group">
            <label class="col-sm-3  control-label"><i class="require">*</i>红包总金额（元）:</label>
            <div class="col-sm-8" style="line-height: 34px;">
                <input class="form-control" name="amount" id="amount" size="30" style="width: 25%;float: left;" value="${payRedEnvRule.amount}" disabled="disabled"/>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3  control-label"><i class="require">*</i>红包总个数:</label>
            <div class="col-sm-8">
                <input class="form-control" name="cNum" id="cNum" size="30" value="${payRedEnvRule.cNum}" onfocus="if(value=='0'){value=''}"  onkeyup="clearNoNum(this)"  onblur="lost1(this)" disabled="disabled"/>
            </div>
        </div>
        <div class="form-group" id="avg">
            <label class="col-sm-3  control-label"><i class="require">*</i>每个红包金额(元):</label>
            <div class="col-sm-8">
                <input class="form-control" name="avgAmount" id="avgAmount" size="30" value="${payRedEnvRule.avgAmount}" onfocus="if(value=='0.00'){value=''}" onkeyup="clearNoNum(this)" onblur="lost2(this)" disabled="disabled"/>
            </div>
        </div>
        <div class="form-group" id="min">
            <label class="col-sm-3  control-label"><i class="require">*</i>红包最小金额(元):</label>
            <div class="col-sm-8">
                <input class="form-control" name="minAmount" size="30" value="${payRedEnvRule.minAmount}" onfocus="if(value=='0.00'){value=''}" onkeyup="clearNoNum(this)" onblur="lost(this)" disabled="disabled"/>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3  control-label"><i class="require">*</i>领取次数限制:</label>
            <div class="col-sm-8" style="line-height: 34px;">
                <span style="font-size: 14px; float: left;">每个用户每天最多领取</span>
                <input class="form-control" name="limitNum" size="30" style="width: 15%;float: left;" value="${payRedEnvRule.limitNum}" onfocus="if(value=='0'){value=''}"  onkeyup="clearNoNum(this)"  onblur="if (value ==''){value='0'}" disabled="disabled"/>
                <span style="font-size: 14px;">个。</span><span style="float: right;margin-right: 15px;">* 设置为0不限制用户领取次数；</span>
            </div>
        </div>
       <div class="form-group">
            <label class="col-sm-3  control-label"><i class="require">*</i>日期范围:</label>
            <div class="col-sm-8">
                <div class="input-group">
                    <input class="form-control col-md-1" name="dateStart" id='dateStart' readonly
                           onclick='WdatePicker()' disabled="disabled"/>
                    <div class="input-group-addon">至</div>
                    <input name='dateEnd' id='dateEnd' readonly
                           class="form-control col-md-1" onclick='WdatePicker()' disabled="disabled"/>
                </div>
            </div>
        </div>
         <div class="form-group">
            <label class="col-sm-3 control-label"><i class="require">*</i>显示时间:</label>
            <div class="col-sm-8">
                <div class="input-group">
                    <input class="form-control col-md-1" name="showTime" id='showTime' readonly disabled="disabled"
                    />
                    <div class="input-group-addon">至</div>
                    <input name='expiredTime' id='expiredTime' readonly disabled="disabled"
                           class="form-control col-md-1"/>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 col-sm-3 control-label"><i class="require">*</i>上架范围:</label>
            <div class="col-sm-8">
                <div class="col-sm-2 m-t2" style="width: 150px;" id="range1">
                    <input type="radio" name="range" size="30"  value="1" disabled="disabled"/><label>公司下全部小区</label>
                </div>
                <div class="col-sm-2 m-t2" style="width: 150px;" id="range2">
                    <input type="radio" name="range"  size="30" value="2" disabled="disabled"/><label>公司下部分小区</label>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">选择投放小区:</label>
            <div class="col-sm-8">
                <select id="acceptCommunityId" name="acceptCommunityId" multiple="multiple" disabled="disabled">
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3  control-label"><i class="require">*</i>金融魔方红包ID:</label>
            <div class="col-sm-8">
                <input class="form-control" name="redId" size="30" />
            </div>
        </div>
    </form>
</div>
<script type="text/javascript">
$(function () {
	if('${payRedEnvRule.dateEnd}' !=''){
    	$("#dateEnd").val('${payRedEnvRule.dateEnd}'.substring(0,10) );
    }else{
	   	$("#dateEnd").val(curDateTime);
    }
	if('${payRedEnvRule.dateStart}' !=''){
    	$("#dateStart").val('${payRedEnvRule.dateStart}'.substring(0,10) );
    }else{
	   	$("#dateStart").val(curDateTime);
    }
   
   	$("input[name='rType'][value='${payRedEnvRule.rType}']").attr("checked", true);
   	$("input[name='range'][value='${payRedEnvRule.range}']").attr("checked", true);
	var rTypeN = ${payRedEnvRule.rType};
   	if(rTypeN == 0){//金额平均
		$("#min").attr("style","display:none");
		$("#avg").attr("style","");
		$("#amount").attr("disabled", "true");
    }else{//金额随机
    	$("#avg").attr("style","display:none");
    	$("#min").attr("style","");
    }
    var range = ${payRedEnvRule.range};
    if(range == 2){
	    addComuId('${payRedEnvRule.acceptAgtCode}');
    }
    
    $("#acceptCommunityId").select2({
        "placeholder": "请选择社区",
        "width": "100%"
    })
    $("#avg").attr("style","display:none");

    $("#showTime,#expiredTime").timepicker({
        timeFormat: 'HH:mm',
        defaultTime: new Date(0, 0, 0, 6, 0, 0),
        interval: 30
    });
    if('${payRedEnvRule.timeStart}' !=''){
    	$("#showTime").val('${payRedEnvRule.timeStart}'.substring(11,16) );
    }
	if('${payRedEnvRule.timeEnd}' !=''){
    	$("#expiredTime").val('${payRedEnvRule.timeEnd}'.substring(11,16) );
    }
    
    $('input:radio[name="rType"]').change( function(){
    	var value = $("input[name='rType']:checked").val();
    		if(value == 0){//金额平均
    			$("#min").attr("style","display:none");
    			$("#avg").attr("style","");
    			$("#amount").attr("disabled", "true");
            }else{//金额随机
            	$("#avg").attr("style","display:none");
            	$("#min").attr("style","");
            	$("#amount").removeAttr("disabled");
            }
    });
    $('input:radio[name="range"]').change( function(){
    	var value = $("input[name='range']:checked").val();
        if(value == 1){
   			$("#acceptCommunityId").attr("disabled", "true");
  			$("#acceptCommunityId").empty();
      		$("#acceptCommunityId").select2({
                  placeholder: "请选择小区",
                  width: "100%"
             })
        }else{//单公司下部分小区
        	$("#acceptCommunityId").removeAttr("disabled");
        	$("#acceptCommunityId").empty();
        	addComuId('${payRedEnvRule.acceptAgtCode}');
        }
    	
    });
    
});
function addComuId(agtCodes){
	var arr='';
	if('${payRedEnvRule.acceptCommunityId}'!=''){
		var comm = '${payRedEnvRule.acceptCommunityId}';
		arr = comm.substring(1,comm.length-1).split(",");
	}
	 $.post("${pageContext.request.contextPath}/admin/tcommunity_listByAgtCodes.action?agtCodes="+agtCodes, function (result) {
           $("#acceptCommunityId").empty();
           $("#acceptCommunityId").append("<option value=''>请选择小区</option>");
            for (var i = 0; i < result.length; i++) {
                $("#acceptCommunityId").append("<option value='" + result[i].sid + "'>" + result[i].communityName + "</option>");
            }
            for (var j=0;j<arr.length;j++) {
            	$("#acceptCommunityId  option[value='" + arr[j] + "'] ").attr("selected", true); 
            }
            $("#acceptCommunityId").select2({
                placeholder: "请选择小区",
                width: "100%"
            })
       }, "json");
}

function clearNoNum(obj)
{
    //先把非数字的都替换掉，除了数字和.
    obj.value = obj.value.replace(/[^\d.]/g,"");
    //必须保证第一个为数字而不是.
    obj.value = obj.value.replace(/^\./g,"");
    //保证只有出现一个.而没有多个.
    obj.value = obj.value.replace(/\.{2,}/g,".");
    //保证.只出现一次，而不能出现两次以上
    obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
}
function lost(obj)
{
	var f = obj.value;  
    if (isNaN(f)) {    
        return false;    
    }    
    obj.value=getNum(f); 
}
function getNum(f){
	    
	    var f = Math.round(f*100)/100;    
	    var s = f.toString();    
	    var rs = s.indexOf('.');    
	    if (rs < 0) {    
	        rs = s.length;    
	        s += '.';    
	    }    
	    while (s.length <= rs + 2) {    
	        s += '0';    
	    }    
	    return s;
}
function lost1(obj)
{
	var f = obj.value;  
    if (isNaN(f)) {    
        return false;    
    }    
    if (f ==''){
	    obj.value='0'; 
    }

    countAmount();
}
function lost2(obj)
{
	var f = obj.value;  
    if (isNaN(f)) {    
        return false;    
    }    
    obj.value=getNum(f);
    countAmount();
}

function countAmount(){
	 var rType = $("input[name='rType']:checked").val();
		if(rType == 0){//金额平均
			var cNum = $("#cNum").val();
			var avgAmount = $("#avgAmount").val();
			var count = getNum(cNum*avgAmount);
			$("#amount").val(count);
		}
}
</script>
</body>
</html>
